package com.wh.usercenter3.interceptor;

import com.wh.usercenter3.constant.CodeConstant;
import com.wh.usercenter3.constant.UserConstant;
import com.wh.usercenter3.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute(UserConstant.LOGIN_USER);

        if (user == null) {
            response.sendRedirect("/user/login");
            log.info("拦截器发现：用户未登录");
            throw new BaseException(CodeConstant.CLIENT_ERR, "用户未登录");

        }
        return true;

    }
}
